#include "Command.hpp"
#include "IntAddr.hpp"

void Usage(const std::string &proc)
{
    std::cout<<"Usage: "<<proc<<" ip port"<<std::endl;
}

int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        exit(USAGE_ERROR);
    }

    std::string serip = argv[1];
    uint16_t serport = std::stoi(argv[2]);

    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
        std::cerr<<"socket error"<<std::endl;
        exit(SOCKET_ERROR);
    }
    InetAddr addr(serip,serport);
    int n = connect(sockfd,addr.GetNetAddr(),addr.GetSocklen());
    if(n<0)
    {
        std::cerr<<"connect error" <<std::endl;
        exit(CONNECT_ERROR);
    }

    while(true)
    {
        std::string line;
        std::cout<<"Please Enter: ";
        std::getline(std::cin,line);

        write(sockfd,line.c_str(),line.size());

        char buffer[1024];
        ssize_t size = read(sockfd,buffer,sizeof(buffer)-1);
        if(size>0)
        {
            buffer[size]=0;
            std::cout<<"Server echo: "<<buffer<<std::endl;
        }
    }
    close(sockfd);
    return 0;
}
